<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: TrackbackDAO.cfc 584 2006-05-16 20:00:29Z mpwoodward $

Notes:
--->
<cfcomponent 
	displayname="TrackbackDAO" 
	output="false" 
	extends="TrackbackDAO" 
	hint="TrackbackDAO for MachBlog (MSSQL)">
	
	<cffunction name="init" access="public" output="false" returntype="TrackbackDAO" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="void" hint="Creates a new trackback">
		<cfargument name="trackback" type="Trackback" required="true" />
		
		<cfset var insertTrackback = "" />
		
		<cfset arguments.trackback.setTrackbackId(CreateUUID()) />
		
		<cftry>
			<cfquery name="insertTrackback" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				INSERT INTO machblog_trackback (
					trackback_id, 
				<cfif arguments.trackback.getTrackbackTitle() NEQ "">
					trackback_title, 
				</cfif>
				<cfif arguments.trackback.getTrackbackExcerpt() NEQ "">
					trackback_excerpt, 
				</cfif>
					trackback_url, 
				<cfif arguments.trackback.getTrackbackBlogName() NEQ "">
					trackback_blog_name, 
				</cfif>
					entry_id, 
				<cfif arguments.trackback.getAudit().getCreatedById() NEQ "">
					created_by_id, 
				</cfif>
					dt_created, 
					ip_created, 
					is_active
				) VALUES (
					<cfqueryparam value="#arguments.trackback.getTrackbackId()#" cfsqltype="cf_sql_char" 
									maxlength="35" />, 
				<cfif arguments.trackback.getTrackbackTitle() NEQ "">
					<cfqueryparam value="#arguments.trackback.getTrackbackTitle()#" 
									cfsqltype="cf_sql_varchar" maxlength="500" />, 
				</cfif>
				<cfif arguments.trackback.getTrackbackExcerpt() NEQ "">
					<cfqueryparam value="#arguments.trackback.getTrackbackExcerpt()#" 
									cfsqltype="cf_sql_longvarchar" />, 
				</cfif>
					<cfqueryparam value="#arguments.trackback.getTrackbackUrl()#" 
									cfsqltype="cf_sql_varchar" maxlength="500" />, 
				<cfif arguments.trackback.getTrackbackBlogName() NEQ "">
					<cfqueryparam value="#arguments.trackback.getTrackbackBlogName()#" 
									cfsqltype="cf_sql_varchar" maxlength="250" />, 
				</cfif>
					<cfqueryparam value="#arguments.trackback.getEntryId()#" cfsqltype="cf_sql_char" 
									maxlength="35" />, 
				<cfif arguments.trackback.getAudit().getCreatedById() NEQ "">
					<cfqueryparam value="#arguments.trackback.getAudit().getCreatedById()#" 
									cfsqltype="cf_sql_char" maxlength="35" />, 
				</cfif>
					<cfqueryparam value="#arguments.trackback.getAudit().getDtCreated()#" cfsqltype="cf_sql_bigint" />, 
					<cfqueryparam value="#arguments.trackback.getAudit().getIpCreated()#" 
									cfsqltype="cf_sql_varchar" maxlength="15" />, 
					<cfqueryparam value="#arguments.trackback.getAudit().getIsActive()#" cfsqltype="cf_sql_bit" />
				)
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
	
	<cffunction name="read" access="public" output="false" returntype="void" hint="Populates a Trackback bean">
		<cfargument name="trackback" type="Trackback" required="true" />
		
		<cfset var getTrackback = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = 0 />
		
		<cftry>
			<cfquery name="getTrackback" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				SELECT 	machblog_trackback.*, 
						cb.first_name AS cbfn, 
						cb.last_name AS cbln, 
						mb.first_name AS mbfn, 
						mb.last_name AS mbln 
				FROM 	machblog_trackback 
				LEFT OUTER JOIN machblog_user AS cb ON machblog_trackback.created_by_id = cb.user_id 
				LEFT OUTER JOIN machblog_user AS mb ON machblog_trackback.modified_by_id = mb.user_id 
				WHERE machblog_trackback.trackback_id = <cfqueryparam value="#arguments.trackback.getTrackbackId()#" 
															cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error [databaseerror]" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfscript>
			// if we got a hit, populate the object
			if (IsQuery(getTrackback) AND getTrackback.RecordCount EQ 1) {
				if (getTrackback.created_by_id NEQ "") {
					cbName = getTrackback.cbfn & " " & getTrackback.cbln;
				} else {
					cbName = getTrackback.name;
				}
				
				if (getTrackback.modified_by_id NEQ "") {
					mbName = getTrackback.mbfn & " " & getTrackback.mbln;
					dtModified = getTrackback.dt_modified;
				}
				
				arguments.trackback.init(getTrackback.trackback_id, getTrackback.trackback_title, getTrackback.trackbackExcerpt, 
											getTrackback.trackback_url, getTrackback.trackbackBlogName, getTrackback.entryId);
				arguments.trackback.getAudit().init(getTrackback.created_by_id, cbName, 
													getTrackback.dt_created, getTrackback.ip_created, 
													getTrackback.modified_by_id, mbName, 
													dtModified, getTrackback.ip_modified, 
													getTrackback.is_active);
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="update" access="public" output="false" returntype="void" hint="Updates a trackback">
		<cfargument name="trackback" type="Trackback" required="true" />
		
		<cfset var updateTrackback = "" />
		
		<cftry>
			<cfquery name="updateTrackback" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				UPDATE 	machblog_trackback 
				SET 	trackback_url = <cfqueryparam value="#arguments.trackback.getTrackbackUrl()#" 
														cfsqltype="cf_sql_varchar" maxlength="500" />, 
						<cfif arguments.trackback.getTrackbackTitle() NEQ "">
							trackback_title = <cfqueryparam value="#arguments.trackback.getTrackbackTitle()#" 
																cfsqltype="cf_sql_varchar" maxlength="500" />, 
							<cfelse>
								trackback_title = null, 
						</cfif> 
						<cfif arguments.trackback.getTrackbackExcerpt() NEQ "">
							trackback_excerpt = <cfqueryparam value="#arguments.trackback.getTrackbackExcerpt()#" 
																cfsqltype="cf_sql_longvarchar" />, 
							<cfelse>
								trackback_excerpt = null, 
						</cfif>
						<cfif arguments.trackback.getTrackbackBlogName() NEQ "">
							trackback_blog_name = <cfqueryparam value="#arguments.trackback.getTrackbackBlogName()#" 
																cfsqltype="cf_sql_varchar" maxlength="250" />, 
							<cfelse>
								trackback_blog_name = null, 
						</cfif>
						entry_id = <cfqueryparam value="#arguments.trackback.getEntryId()#" 
												cfsqltype="cf_sql_char" maxlength="35" />, 
						<cfif arguments.trackback.getAudit().getModifiedById() NEQ "">
							modified_by_id = <cfqueryparam value="#arguments.trackback.getAudit().getModifiedById()#" 
															cfsqltype="cf_sql_char" maxlength="35" />, 
						</cfif>
						dt_modified = <cfqueryparam value="#arguments.trackback.getAudit().getDtModified()#" 
													cfsqltype="cf_sql_bigint" />, 
						ip_modified = <cfqueryparam value="#arguments.trackback.getAudit().getIpModified()#" 
													cfsqltype="cf_sql_varchar" maxlength="15" />
						is_active = <cfqueryparam value="#arguments.trackback.getAudit().getIsActive()#" 
													cfsqltype="cf_sql_bit" />, 
				WHERE 	trackback_id = <cfqueryparam value="#arguments.trackback.getTrackbackId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
	
	<cffunction name="delete" access="public" output="false" returntype="struct" hint="Deletes a trackback">
		<cfargument name="trackback" type="Trackback" required="true" />
		
		<cfset var deleteTrackback = "" />
		
		<cftry>
			<cfquery name="deleteTrackback" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" 
					password="#getDatasource().getPassword()#">
				DELETE FROM machblog_trackback 
				WHERE trackback_id = <cfqueryparam value="#arguments.trackback.getTrackbackId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
</cfcomponent>
